package com.yonyou.marketing.app.api.controller;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.AppResponse;
import org.marketing.common.constants.ResultMsgConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmmc.common.annotation.ParamValid;
import com.yonyou.marketing.app.api.constant.AppConstant;
import com.yonyou.marketing.app.api.entity.GscReservation;
import com.yonyou.marketing.app.api.service.AppGetCarService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author Administrator
 * @date 2017年6月30日
 */
@RestController
@RequestMapping(value = AppConstant.BASE_PATH + "/" + AppConstant.VERSION)
@Api("APP取送车API")
public class AppGetCarController {

	private Logger          logger = Logger.getLogger(AppGetCarController.class);

	@Autowired
    private AppGetCarService appGetCarService;
	
	/**
	 * 判断是否有取送车权限
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/isCanGetSendCar", method = RequestMethod.POST)
	@ApiOperation(value = "判断是否有取送车权限", notes = "判断是否有取送车权限")
	@ParamValid
	public AppResponse<Map<String, Object>> isCanGetSendCar(@RequestParam(required=false,defaultValue="") String dealerCode) {
		logger.info("AppAppGetCarController.isCanGetSendCar==>start...");
		try {
			Map<String, Object> list = appGetCarService.isCanGetSendCar(dealerCode);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, list, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppAppGetCarController.isCanGetSendCar==>end...");
		}
	}
	/**
	 * 向Idealer下单，积分支付
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/saveGscReservation", method = RequestMethod.POST)
	@ApiOperation(value = "向Idealer下单，积分支付", notes = "向Idealer下单，积分支付")
	@ParamValid
	public AppResponse<Map<String, Object>> saveGscReservation(@RequestParam(required=true,defaultValue="") String pkId) {
		logger.info("AppGetCarController.saveGetOrSendCar==>start...");
		try {
			Map<String, Object> map = appGetCarService.saveGetOrSendCar(pkId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.saveGetOrSendCar==>end...");
		}
	}
	/**
	 * 向Idealer下单，微信支付
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/saveWxOrder", method = RequestMethod.POST)
	@ApiOperation(value = "向idealer下单，并微信支付", notes = "向idealer下单，并微信支付")
	@ParamValid
	public AppResponse<Map<String, Object>> saveWxOrder(@RequestParam(required=true,defaultValue="") String pkId) {
		logger.info("AppGetCarController.saveWxOrder==>start...");
		try {
			Map<String, Object> map = appGetCarService.saveWxOrder(pkId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.saveWxOrder==>end...");
		}
	}
	/**
	 * 向Idealer下单，微信支付成功，状态更新
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/updateWxState", method = RequestMethod.POST)
	@ApiOperation(value = "微信支付成功，状态更新", notes = "微信支付成功，状态更新")
	@ParamValid
	public AppResponse<Map<String, Object>> updateWxState(@RequestParam(required=true,defaultValue="") String pkId
			,@RequestParam(required=true,defaultValue="") String sign) {
		logger.info("AppGetCarController.updateWxState==>start...");
		try {
			Map<String, Object> map = appGetCarService.updateWxState(pkId,sign);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.updateWxState==>end...");
		}
	}
	/**
	 * 取消取送车
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/cancelCar", method = RequestMethod.POST)
	@ApiOperation(value = "取消取送车", notes = "取消取送车")
	public AppResponse<Map<String, Object>> cancelCar(@RequestParam(required=false,defaultValue="") String etcoId) {
		logger.info("AppGetCarController.cancelGetSendCar==>start...");
		try {
			
			Map<String, Object> map = appGetCarService.cancelGetSendCar(etcoId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.cancelGetSendCar==>end...");
		}
	}
	/**
	 * 取送车 快捷获取地址
	 * @param searchType  1:首选销售店   2:家  0:地图输入地址
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/searchAddr", method = RequestMethod.POST)
	@ApiOperation(value = "快捷获取地址", notes = "快捷获取地址")
	@ParamValid
	public AppResponse<Map<String, Object>> searchAddr(@RequestParam(required=false,defaultValue="1") Integer searchType
			,@RequestParam(value = "userId", required = false) String userId) {
		logger.info("AppGetCarController.searchAddr==>start...");
		try {
			Map<String, Object> map = appGetCarService.searchAddr(searchType,userId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.searchAddr==>end...");
		}
	}
	/**
	 * 取送车 获取历史地址
	 * @param addrName  搜索地址名称
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/searchHistory", method = RequestMethod.POST)
	@ApiOperation(value = "获取历史地址", notes = "获取历史地址")
	@ParamValid
	public AppResponse<List<Map<String, Object>>> searchHistory(@RequestParam(required=false,defaultValue="") String addrName
			,@RequestParam(value = "userId", required = false) String userId) {
		logger.info("AppGetCarController.searchHistory==>start...");
		try {
			List<Map<String, Object>> list = appGetCarService.searchHistory(addrName,userId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, list, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.searchHistory==>end...");
		}
	}
	@RequestMapping(value = "/getCarInfo", method = RequestMethod.POST)
	@ApiOperation(value = "获取车主信息", notes = "获取车主信息")
	public AppResponse<Map<String, Object>> getCarInfo(@RequestParam(value = "userId", required = false) String userId) {
		logger.info("AppGetCarController.validateGscDistance==>start...");
		try {
			Map<String, Object> map = appGetCarService.getCarInfo(userId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.validateGscDistance==>end...");
		}
	}
	/**
	 * 取送车 判断距离是否超出服务范围
	 * @param addrName  搜索地址名称
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/validateGscDistance", method = RequestMethod.POST)
	@ApiOperation(value = "判断距离是否超出服务范围", notes = "判断距离是否超出服务范围")
	public AppResponse<Map<String, Object>> validateGscDistance(
			 @RequestParam(value = "getSendFlag", required = true) Integer getSendFlag
			,@RequestParam(value = "model", required = true) String model
			,@RequestParam(value = "licenseNo", required = true) String licenseNo
			,@RequestParam(value = "destDate", required = true) String destDate
			,@RequestParam(value = "getSendAddr", required = true) String getSendAddr
			,@RequestParam(value = "getSendAddrType", required = true) String getSendAddrType
			,@RequestParam(value = "phone", required = true) String phone
			,@RequestParam(value = "bournAddr", required = true) String bournAddr
			,@RequestParam(value = "bournAddrType", required = true) String bournAddrType
			,@RequestParam(value = "getUserPhone", required = true) String getUserPhone
			,@RequestParam(value = "srvOrderNo", required = false) String srvOrderNo
			,@RequestParam(value = "coupons", required = false) String coupons
			,@RequestParam(value = "msgSrcService", required = true) Integer msgSrcService
			,@RequestParam(value = "userId", required = false) String userId) {
		logger.info("AppGetCarController.validateGscDistance==>start...");
		try {
			GscReservation gscRes = new GscReservation();
			gscRes.setGetSendFlag(getSendFlag);
			gscRes.setModel(model);
			gscRes.setLicenseNo(licenseNo);
			gscRes.setDestDate(destDate);
			gscRes.setGetSendAddr(getSendAddr);
			gscRes.setGetSendAddrType(getSendAddrType);
			gscRes.setPhone(phone);
			gscRes.setBournAddr(bournAddr);
			gscRes.setBournAddrType(bournAddrType);
			gscRes.setGetUserPhone(getUserPhone);
			gscRes.setSrvOrderNo(srvOrderNo);
			gscRes.setCoupons(coupons);
			gscRes.setMsgSrcService(msgSrcService);
			Map<String, Object> map = appGetCarService.validateGscDistance(gscRes,userId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.validateGscDistance==>end...");
		}
	}
    /**
	 * 我的取送车  列表
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/queryCar", method = RequestMethod.POST)
	@ApiOperation(value = "查看取送（车）订单列表", notes = "查看取送（车）订单列表")
	public AppResponse<List<Map<String, Object>>> queryCar(@RequestParam(value = "userId", required = false) String userId) {
		logger.info("AppGetCarController.queryCar==>start...");
		try {
			List<Map<String, Object>> list = appGetCarService.queryCar(userId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, list, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.queryCar==>end...");
		}
	}
	/**
	 * 我的取送车  详情
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/queryQscDetail", method = RequestMethod.POST)
	@ApiOperation(value = "查看取送（车）订单详情", notes = "查看取送（车）订单详情")
	public AppResponse<Map<String, Object>> queryQscDetail(@RequestParam(required=true,defaultValue="") String etcoId) {
		logger.info("AppGetCarController.queryQscDetail==>start...");
		try {
			Map<String, Object> map = appGetCarService.queryQscDetail(etcoId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.queryQscDetail==>end...");
		}
	}
	/**
	 * 我的取送车  查看进度
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/queryQscProgress", method = RequestMethod.POST)
	@ApiOperation(value = "查看进度", notes = "查看进度")
	public AppResponse<Map<String, Object>> queryQscProgress(@RequestParam(required=true,defaultValue="") String etcoId) {
		logger.info("AppGetCarController.queryQscProgress==>start...");
		try {
			Map<String, Object> map = appGetCarService.queryQscProgress(etcoId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.queryQscProgress==>end...");
		}
	}
	/**
	 * 我的取送车  查看轨迹信息
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/queryQscTrack", method = RequestMethod.POST)
	@ApiOperation(value = "查看轨迹信息", notes = "查看轨迹信息")
	public AppResponse<Map<String, Object>> queryQscTrack(@RequestParam(required=true,defaultValue="") String etcoId) {
		logger.info("AppGetCarController.queryQscTrack==>start...");
		try {
			Map<String, Object> map = appGetCarService.queryQscTrack(etcoId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.queryQscTrack==>end...");
		}
	}
	/**
	 * 我的取送车  查看司机信息
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/queryDriverinfo", method = RequestMethod.POST)
	@ApiOperation(value = "查看司机信息", notes = "查看司机信息")
	public AppResponse<Map<String, Object>> queryDriverinfo(@RequestParam(required=true,defaultValue="") String etcoId) {
		logger.info("AppGetCarController.queryDriverinfo==>start...");
		try {
			Map<String, Object> map = appGetCarService.queryDriverinfo(etcoId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppGetCarController.queryDriverinfo==>end...");
		}
	}
}
